<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <i class="iconfont icon-touxiang"
           style="font-size:30px;color: #00A1D8"></i>
      </div>
      <div class="sysName">HAWK</div>
      <!-- 登录表单区域 -->
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules"
               label-width="0px" class="login_form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入账号"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" placeholder="请输入密码"
                    type="password"></el-input>
        </el-form-item>
        <el-checkbox-group v-model="loginForm.remember"
                           style="margin-bottom:24px;">
          <el-checkbox label="记住我" :name="'0'"></el-checkbox>
        </el-checkbox-group>
        <!-- 按钮区域 -->
        <el-button type="primary" style="width:100%;background:#00A1D8"
                   @click="login">登录</el-button>
      </el-form>
    </div>
  </div>
</template>
 
<script>
/*
* @author 宋辉辉
* @description 用户登录的视图层
* @api 无
* @since 2021年10月13日15:17
* @update 2021年11月2日11:20
* @modifier 宋辉辉
* @bug 无
* @version v1.1.0
*/
import routes from '@/utils/routerList'
import { service, login } from '@/utils/request'
export default {
  data () {
    return {
      isSaveMe: '',

      // 这是登录表单的数据绑定对象
      loginForm: {
        username: 'admin', // admin
        password: '123456', // 123456
        remember: []
      },
      // 这是表单的验证规则对象
      loginFormRules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur'
          }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // 点击重置按钮，重置登录表单
    resetLoginForm () {
      // console.log(this);
      this.loginForm = this.$options.data().loginForm
    },

    checkSaveUser () {




    },
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        // const {data:res} = await this.$http.post('/users',this.loginForm);

        // 真实的登陆
        let res = await login({
          url: `/login`,
          data: {
            username: this.loginForm.username,
            password: this.loginForm.password
          },


        })

        console.log(res);
        if (res.code === 20000) {

          this.checkSaveUser()

          // 保存用户名，密码
          if (this.loginForm.remember.length > 0) {
            // 
            console.log('----------记住我-----');
            localStorage.setItem('save_username', this.loginForm.username)
            localStorage.setItem('save_password', this.loginForm.password)
            localStorage.setItem('save_remember', true)
          } else {
            localStorage.setItem('save_remember', '')
             localStorage.setItem('save_username', '')
            localStorage.setItem('save_password', '')
            console.log('------清空------');
          }


          this.$store.dispatch('user/setUser', res.data.userInfo)

          this.$store.dispatch('user/setToken', res.data.access_token)
          // const {data : res1} = await this.$http.get("/menu");
          this.$store.dispatch('menu/setHead', routes.data)

          this.$message({
            duration: 500,
            message: '登录成功',
            type: 'success'
          })

          this.$router.push('/')

        } else {
          this.$message({
            duration: 1000,
            message: res.message,
            type: 'error'
          })
        }


        console.log("-------------");
        // console.log(reqForm);
        // console.log(data1);


      })
    }
  },

  created () {

    if (localStorage.getItem('save_remember')) {
      console.log('------读取数据-------');
      this.loginForm.username = localStorage.getItem('save_username')
      this.loginForm.password = localStorage.getItem('save_password')
      this.loginForm.remember[0] = '记住我'
    }else{
      console.log('------没有读到数据-------');
    }

  }

}
</script>

<style lang="less" scoped>
.el-form-item {
  margin-bottom: 20px;
}
.login_container {
  height: 100%;
  background-image: url(../../assets/images/loginBack.png);
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

.login_box {
  width: 400px;
  height: 430px;
  background: #ffffff;
  box-shadow: 0px 2px 4px -2px rgba(16, 24, 40, 0.06),
    0px 4px 8px -2px rgba(16, 24, 40, 0.1);
  border-radius: 12px 12px 12px 12px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 48px;
    width: 48px;
    border-radius: 6px;
    background: rgba(16, 24, 40, 0.06);
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 0 auto;
    margin-top: 32px;
    box-shadow: 0px 1px 3px 1px rgba(16, 24, 40, 0.06);
  }
  .sysName {
    width: 100%;
    margin: 24px 0;
    font-size: 24px;
    font-family: Source Han Sans CN-Bold, Source Han Sans CN;
    font-weight: bold;
    color: #333333;
    line-height: 32px;
    text-align: center;
  }
}

.login_form {
  width: calc(100% - 80px);
  padding: 0 40px;
}
</style>
